package io.github.jyrmc.deploy.utils

import io.github.jyrmc.deploy.entity.KubernetesNamespaceEntity
import io.github.jyrmc.deploy.entity.PodEntity

object DataUtils {

    fun getNamespaces(data: String): MutableList<KubernetesNamespaceEntity> {
        if (data.isBlank()) {
            return mutableListOf()
        }

        val results = mutableListOf<KubernetesNamespaceEntity>()
        val lineData = data.split("\n")
        for (i in lineData.indices) {
            if (i == 0 || lineData[i].isBlank()) {
                continue
            }
            val tempArray: MutableList<String> = mutableListOf()
            val data: Array<String> = lineData[i].split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            data.forEach {
                if (it.isNotBlank()) {
                    tempArray.add(it)
                }
            }
            results.add(KubernetesNamespaceEntity(name = tempArray[0], status = tempArray[1], age = tempArray[2]))
        }

        return results;
    }

    fun getPodInfo(data: String): MutableList<PodEntity> {
        if (data.isBlank()) {
            return mutableListOf()
        }

        val results = mutableListOf<PodEntity>()
        val lineData = data.split("\n")
        for (i in lineData.indices) {
            if (i == 0 || lineData[i].isBlank()) {
                continue
            }
            val tempArray: MutableList<String> = mutableListOf()
            val data: Array<String> = lineData[i].split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            data.forEach {
                if (it.isNotBlank()) {
                    tempArray.add(it)
                }
            }
            results.add(
                PodEntity(
                    name = tempArray[0],
                    ready = tempArray[1],
                    upToDate = tempArray[2].toInt(),
                    available = tempArray[3].toInt(),
                    age = tempArray[4]
                )
            )
        }

        return results;
    }
}